/**
 * @file main
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <locale.h>
#include <windows.h>

#include "lvgl/lvgl.h"
#include "lv_drivers/win32drv/win32drv.h"
#include "lvgl/demos/music/lrc_parser.h"
#include "lvgl/demos/music/utf-8_to_gbk.h"
#include "lvgl/demos/music/mp3_import.h"
#include "lvgl/demos/music/lv_demo_music.h"
#include "lvgl/demos/music/sensor_read.h"

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void hal_init(void);
static int tick_thread(void *data);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nCmdShow)
{


    //设置控制台为中文
    SetConsoleOutputCP (65001);
    // 设置本地化支持中文路径
    setlocale(LC_ALL, "zh-CN.UTF-8");

    /*Initialize LittlevGL*/
    lv_init();

    /*Initialize the HAL for LittlevGL*/
    lv_win32_init(hInstance, SW_SHOWNORMAL, 250, 250, NULL);

    /*初始化串口*/
    init_serial_port();

    /*运行*/
    /*Run music player*/
    lv_demo_music();

    /*Main loop*/
    while(!lv_win32_quit_signal) {
        /* 从串口读取运动数据 */
        uint8_t current_motion = read_serial_motion_data();

        // 打印状态信息（调试用）
        print_sensor_data();

        /* Handle LVGL tasks */
        lv_task_handler();

        /* Short delay */
        usleep(50000);  // 50ms delay, simulating real-time system
    }

    // 关闭串口
    close_serial_port();

    // Free lyrics resources
    return 0;
}
